package test.groovy

import groovy.transform.ToString
import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.transform.ASTTransformation
import org.codehaus.groovy.transform.GroovyASTTransformation
import org.codehaus.groovy.transform.GroovyASTTransformationClass

import java.lang.annotation.*

@Target([ElementType.TYPE])
@Documented
@Retention(RetentionPolicy.RUNTIME)
//@GroovyASTTransformationClass(["test.groovy.WithLoggingASTTransformation"])
//@GroovyASTTransformationClass({"test.groovy.WithLoggingASTTransformation"})
@GroovyASTTransformationClass(["test.groovy.WithLoggingASTTransformation", "org.codehaus.groovy.transform.ToStringASTTransformation"])
public @interface WithLoggin {}

@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION)
class WithLoggingASTTransformation implements ASTTransformation {
    @Override
    void visit(ASTNode[] nodes, SourceUnit sourceUnit) {
        def method = nodes[1]
        System.out.println method
        /*   def startMessage = createPrintlnAst("Starting")
           def endMessage = createPrintlnAst("Ending")
           def existingStatements = ((BlockStatement) method.code).statements
           existingStatements.add(0, startMessage)
           existingStatements.add(endMessage)*/


    }

    /* private Statement createPrintlnAst(String message) {
         def strings = new ArrayList<String>()
         new ExpressionStatement(
                 new MethodCallExpression(
                         new VariableExpression(new KeyPath(strings)),
                         new ConstantExpression("println"),
                         new ArgumentListExpression(
                                 new ConstantExpression(message)
                         )
                 )
         )
     }*/
}


def greet() {
    println "Hello World"
}

@ToString
@WithLoggin
class DD {
    String str;
}

greet()